import time
from food import Food



INIT_SNAKESIZE = 3
DIR_SNAKE_UP = 0
DIR_SNAKE_DOWN = 1
DIR_SNAKE_LEFT = 2
DIR_SNAKE_RIGHT = 3
INIT_DIR = DIR_SNAKE_RIGHT


class Snake:
    is_dead = False
    grid_x = 0
    grid_y = 0
    grid_size = 1
    grids = []
    last_tail_grid = (0, 0)
    dir = 0
    move_speed = 50
    move_ts = 0

    def __init__(self, screen_x, screen_y, grid_size) -> None:
        # 出生在居中点
        born_x = int(screen_x / grid_size / 2)
        born_y = int(screen_y / grid_size / 2)
        self.grid_x = int(screen_x / grid_size)
        self.grid_y = int(screen_y / grid_size)
        x, y = born_x, born_y
        self.grids = [(born_x, born_y)]
        self.dir = INIT_DIR
        self.grid_size = grid_size
        opp_dir = self.get_opp_dir(INIT_DIR)
        for i in range(0, INIT_SNAKESIZE):
            x, y = self.get_next_pos(self.grid_x, self.grid_y, opp_dir, (x, y))
            self.grids.append((x, y))
            self.last_tail_grid = (x, y)
        
    def eat(self):
        self.grids.append(self.last_tail_grid)

    @staticmethod
    def get_timestamp():
        return int(time.time() * 1000)

    @staticmethod
    def get_opp_dir(dir):
        if dir == DIR_SNAKE_UP:
            return DIR_SNAKE_DOWN
        elif dir == DIR_SNAKE_DOWN:
            return DIR_SNAKE_UP
        elif dir == DIR_SNAKE_LEFT:
            return DIR_SNAKE_RIGHT
        elif dir == DIR_SNAKE_RIGHT:
            return DIR_SNAKE_LEFT

    @staticmethod
    def get_next_pos(grid_x, grid_y, dir, pos):
        x, y = pos
        if dir == DIR_SNAKE_UP:
            next_x, next_y  = x, y - 1
        elif dir == DIR_SNAKE_DOWN:
            next_x, next_y  = x, y + 1
        elif dir == DIR_SNAKE_LEFT:
            next_x, next_y  = x - 1, y
        elif dir == DIR_SNAKE_RIGHT:
            next_x, next_y  = x + 1, y
        else:
            next_x, next_y = x, y

        if next_x < 0:
            next_x = grid_x + next_x + 1
        elif next_x > grid_x:
            next_x = next_x - grid_x - 1
        else:
            pass

        if next_y < 0:
            next_y = grid_y + next_y + 1
        elif next_y > grid_y:
            next_y = next_y - grid_y - 1
        else:
            pass

        return next_x, next_y

    def get_grids(self):
        return self.grids

    def get_draw_grids(self): 
        grid_size = self.grid_size
        draw_grids = [(x * grid_size, y * grid_size) for x, y in self.grids]
        return draw_grids


    def auto_move(self):
        now_time = self.get_timestamp()
        if now_time - self.move_ts >= self.move_speed:
            self.move_ts = now_time
            grid_x = self.grid_x
            grid_y = self.grid_y
            dir = self.dir
            last_tail_grid = self.grids[-1:][0]
            self.grids = self.grids[:-1] # 去旧的尾巴
            grids = self.grids
            head = self.grids[:1][0]
            new_head = self.get_next_pos(grid_x, grid_y, dir, head)
            if new_head in grids:
                self.is_dead = True
                return
            else:
                self.grids.insert(0, new_head)
                self.last_tail_grid = last_tail_grid
        
    def change_dir(self, dir):
        if self.get_opp_dir(dir) == self.dir:
            pass
        else:
            self.dir = dir

            


                
                